// 获取Cookie
function getCookie(cname) {
	var name = cname + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i].trim();
		var arrC = c.split("=");
		if (arrC[0] == cname) {
			return arrC[1];
		}
	}
	return "";
}
//设置Cookie
function setCookie(cname, cvalue, exdays) {
	var d = new Date();
	d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
	var expires = "expires=" + d.toGMTString();
	document.cookie = cname + "=" + cvalue + "; " + expires;
}
$(document).ready(function() {
	var cart = getCookie("cart");
	var goodsObj = countPrice();
	if (cart) {
		var goodsArr = JSON.parse(cart);
		var goodsIndex = goodsObj.index;
		var goodsPrice = goodsObj.price;
		var goodsNum = goodsObj.goodsNum;
		var _html = '';
		goodsArr.forEach(function(item) {
			var itemPrice = item.goodsPrice.replace(/￥/g, "").trim();
			var _prcie = Number(item.goodsPrice.replace(/￥/g, "") * Number(item.num));
			_html +=
				'<div class="cart_info clearfix"><div class="cart_header_item ca_w8 fl">' +
				'<label>' +
				'<input type="checkbox" name="" class="cheack_btn" value="" /><span class="padding_text"></span>' +
				'</label></div>' +
				'<div class="cart_header_item ca_w42 fl">' +
				'<div class="cart_img fl">' +
				'<img src="' + item.goodsImg + '" ></div>' +
				'<div class="cart_text fl">' +
				'<a class="cart_text_title" href="">' + item.goodsName + '</a>' +
				'<p class="cart_text_type"></p>' +
				'<p class="cart_text_info">' + item.goodsInfo + '</p></div></div>' +
				'<div class="cart_header_item ca_w8 fl">' + itemPrice + '</div>' +
				'<div class="cart_header_item ca_w16 fl">' +
				'<div class="add fl"><button type="button">-</button></div>' +
				'<div class="cart_num fl"><input type="text" name="" value="' + item.num + '" />' +
				'</div><div class="reduce fl"><button type="button">+</button></div></div>' +
				'<div class="cart_header_item ca_w8 paddingNone fl"><p class="subtotal">' + _prcie.toFixed(2) +
				'</p></div>' +
				'<div class="cart_header_item ca_w16 fl"><p class="deleted">删除</p><p class="collect">移到收藏夹</p></div>' +
				'</div>';
			$(".total_price").text("0.00");
			$(".total_num").text("0");
			$(".cartNum").text(goodsNum);
		})
		$('.cart_content').html(_html);
	}
    cartBar();
})


//全选
$(".cart_content").on("click", ".cheack_btn", function() {
	var checked = $(".cart_content").find(".cheack_btn:checked");
	var checkedLenght =  checked.length;
	var checkboxLength = $(".cart_content").find(".cheack_btn").length;
	Array.from($('.checkAll')).forEach(function(item) {
		$(item).prop("checked", checkboxLength == checkedLenght);
	});
	pay(checkedLenght);
	allprice();
	
})
$('.checkAll').click(function() {
	$(".cart_content").find(".cheack_btn").prop("checked", $(this).prop("checked"));
	$('.checkAll').prop("checked", $(this).prop("checked"));
	pay($(".cart_content").find(".cheack_btn:checked").length);
	allprice()
})

function pay(checkedLenght) {
	if (checkedLenght > 0) {
		$(".settlement").removeClass("set_style");
		$(".settlement a").prop("href", "payment.html");
	} else {
		$(".settlement").addClass("set_style");
		$(".settlement a").removeAttr("href");
		$(".total_price").text("0.00");
		$(".total_num").text("0")
	};
	$(".cart_content").find(".cart_info").css({
		background:"#FFFFFF"
	})
	Array.from($(".cart_content").find(".cheack_btn:checked")).forEach(function(item){
		$(item).parent().parent().parent().css({
			background:"#fffeec"
		})
	})
}

//添加商品数量  修改cookie
$(".cart_content").on("click", ".reduce", function() {
	 addNum(1,$(this));
})
$(".cart_content").on("click", ".add", function() {
	 addNum(-1,$(this));
})
function addNum(num,item){
	var car_index = item.parent().parent().index();
	var cart = getCookie("cart");
	var goodsArr = JSON.parse(cart);
	var goods_num = item.siblings().find("input").val();
	
	goods_num = Number(goods_num)+Number(num)*1
	if(goods_num<1){
		return;
	}
	item.siblings().find("input").val(goods_num);
	goodsArr[car_index].num = goods_num;
	setCookie('cart', JSON.stringify(goodsArr));
	var price = goodsArr[car_index].goodsPrice.replace(/￥/g,"").trim();
	var text = goods_num*parseFloat(price);
	item.parent().parent().find(".subtotal").text(text.toFixed(2));
	allprice();
	anew();
}
function allprice(){
	var checked = $(".cart_content").find(".cheack_btn:checked");
	var pricesum = 0;
	var goodssum = 0;
	Array.from(checked).forEach(function(item){
	var $subtotal = $(item).parent().parent().parent().find(".subtotal");
	var $cart_num = $(item).parent().parent().parent().find(".cart_num>input");
	pricesum = pricesum + parseFloat($subtotal.text());
	console.log(pricesum)
	goodssum = goodssum + parseFloat($cart_num.val());
		$(".total_price").text(pricesum.toFixed(2));
		$(".total_num").text(goodssum);
	})
}
function anew(){
	var sum = 0;
	Array.from($(".cart_info")).forEach(function(item){
	sum = sum + parseInt($(item).find(".cart_num>input").val());
	$(".cartNum").text(sum);
	});
}
//删除
$(".cart_content").on("click",".deleted",function(){
	var $item = $(this).parent().parent();
	var _itemIndex = $item.index();
	var cart = getCookie("cart");
	var goodsArr = JSON.parse(cart);
	goodsArr.splice(_itemIndex, 1);
	setCookie('cart', JSON.stringify(goodsArr));
	$item.remove();
	anew();
	cartBar();
})

$(".cart_deleted").click(function(){
	var checked = $(".cart_content").find(".cheack_btn:checked");
	Array.from(checked).forEach(function(item){
		var $item = $(item).parent().parent().parent();
		var _itemIndex = $item.index();
		var cart = getCookie("cart");
		var goodsArr = JSON.parse(cart);
		goodsArr.splice(_itemIndex, 1);
		setCookie('cart', JSON.stringify(goodsArr));
		$item.remove();
	anew(cart,goodsArr);
	})
	cartBar();
})

function cartBar(){
	if( $(".cart_content").children().length==0){
		$(".main_content").css({
			display:"none"
		});
		$(".jumbotron").css({
			display:"block"
		});
		return;
	}
	$(".main_content").css({
		display:"block"
	});
	$(".jumbotron").css({
		display:"none"
	});
}


